#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @time     : 2019/7/4 15:48
# @Author   : BYF
# file      : get_excel.py
# @version  :

import os
import xlrd
from openpyxl.reader.excel import load_workbook

from config import config

class getData:

    def __init__(self,file_name,sheet_num):
        # 类的定义全局变量
        self.data_path=config.data_path
        self.file_name=file_name
        self.sheet_num=sheet_num

        self.data_file = os.path.join(self.data_path, self.file_name)
        # print(self.data_file)
        self.wbook = xlrd.open_workbook(self.data_file)
        self.sheet = self.wbook.sheet_by_index(self.sheet_num)

    def readaApiMeg(self):
        sheet=self.sheet #调用内部方法，获取sheet内容
        header=sheet.row_values(6) #获取用例表头：固定行
        #获取测试用例：for循环将测试用例名称和用例通过字典键值对的方式获取，并将需要信息赋值给megs
        megs = []
        # print(sheet.nrows)
        row =-1
        for i in range(7,sheet.nrows):
            data=dict(zip(header,sheet.row_values(i)))
            # print(data)
            testname=data['用例名称']
            url = sheet.row_values(3)[1]

            try:
                param = eval(data['请求参数'])  # 将str转变为dict
            except SyntaxError:
                param = data['请求参数'] #参数为空

            status = data['status']
            if type(status) == type(1):
                status = int(status)

            message = data['message']
            if type(message) == type(''):
                message = str(message)

            header1 = sheet.row_values(5)[1]
            header2 = {'keys': data['keys']}
            if len(header1)>0 and len(header2['keys'])>0:
                header1=eval(header1)
                headers = dict(header1)
                headers.update(header2)  # 将多个字典合并成一个
            elif len(header1)>0 and len(header2['keys'])==0:
                header1=eval(header1)
                headers = dict(header1)
            elif len(header1)==0 and len(header2['keys'])>0:
                headers = header2
            else:
                headers = None

            row=row+1
            # 将接口信息拼接后返回
            meg = {
                'row':row,
                'testname':testname,
                'url': url,
                'headers': headers,
                'method': data['请求方式'],
                'data': param,
                'status_code':data['status_code'],
                'status':status,
                'message':message
                }

            megs.append(meg)
        return megs



class writeData:

    def __init__(self, file_name, sheet_num):
        self.data_path=config.data_path
        self.data_file = os.path.join(self.data_path, file_name)
        self.wbook = load_workbook(self.data_file)
        sheets = self.wbook.sheetnames
        self.sheet = self.wbook[sheets[sheet_num]]

    def writeTestRes(self,row,test_result,error_msg):
        sheet=self.sheet #调用内部方法，获取sheet内容

        sheet.cell(row=row+8, column=10, value=test_result)
        sheet.cell(row=row+8, column=11, value=error_msg)

        self.wbook.save(self.data_file) #保存工作簿


if __name__=='__main__':
    file_name='中台-会员卡模板-POST apiv1templateupdateTemplate 【PC】新增修改会员卡模板.xlsx'
    # file_name = '登录接口测试用例.xlsx'
    sheet_num=0
    # writeData(file_name, sheet_num).writeTestRes(api_meg['ro'], test_result,
    #                                                                                      str(error_msg))
    print(getData(file_name,sheet_num).readaApiMeg())


